#include "at32f421_board.h"
#include "at32f421_clock.h"

#include "iot_button.h"
#include "eeprom.h"
#include "temperature_adc.h"

crm_clocks_freq_type crm_clocks_freq_struct = {0};

int16_t LEDA_value = 600;
int16_t LEDB_value = 600;

uint8_t baoshan_flag = 0;
uint8_t baoshan_i;

uint8_t save_flag = 0;

void led_gpio_init(void)
{
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
	
	gpio_init_type gpio_init_struct;
	
	gpio_default_para_init(&gpio_init_struct);
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	
	gpio_init(GPIOB, &gpio_init_struct);
	
	gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE4, GPIO_MUX_1);
	gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE5, GPIO_MUX_1);
}

void ok_led_init(void)
{
	crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
	
	gpio_init_type gpio_init_struct;
	
	gpio_default_para_init(&gpio_init_struct);
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
	gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_pins = GPIO_PINS_15;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	
	gpio_init(GPIOA, &gpio_init_struct);
	
	//gpio_bits_write(GPIOA ,GPIO_PINS_15, TRUE);
}

void led_tmr_init(void)
{
	crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
	
	tmr_output_config_type tmr_output_struct;
	tmr_base_init(TMR3, 2999, 7);
	tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
	
  tmr_output_default_para_init(&tmr_output_struct);
  tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
  tmr_output_struct.oc_output_state = TRUE;
  tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
  tmr_output_struct.oc_idle_state = TRUE;
  tmr_output_struct.occ_output_state = TRUE;
  tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
  tmr_output_struct.occ_idle_state = FALSE;
	
  tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
  tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, 0);
	
  tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
  tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, 0);
	
  /* output enable */
  tmr_output_enable(TMR3, TRUE);

  /* enable tmr1 */
  tmr_counter_enable(TMR3, TRUE);
}

static void btnAadd_single_click_cb(void *arg, void *usr_data)
{
	LEDA_value += 600;
	
	if(LEDA_value > 2400)
	{
		LEDA_value = 2400;
	}
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, LEDA_value);
}

static void btnAremove_single_click_cb(void *arg, void *usr_data)
{
	LEDA_value -= 600;
	
	if(LEDA_value < 0)
	{
		LEDA_value = 0;
	}
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, LEDA_value);
}

static void btnBadd_single_click_cb(void *arg, void *usr_data)
{
	LEDB_value += 600;
	
	if(LEDB_value > 2400)
	{
		LEDB_value = 2400;
	}
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, LEDB_value);
}

static void btnBremove_single_click_cb(void *arg, void *usr_data)
{
	LEDB_value -= 600;
	
	if(LEDB_value < 0)
	{
		LEDB_value = 0;
	}
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, LEDB_value);
}

static void btnfun_baoshan_cb(void *arg, void *usr_data)
{
	baoshan_flag = !baoshan_flag;
	if(!baoshan_flag)
	{
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, LEDA_value);
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, LEDB_value);
	}
}

static void btnfun_sleep_cb(void *arg, void *usr_data)
{
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, 0);
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, 0);
	delay_ms(10);
  /* enter standby mode */
  pwc_standby_mode_enter();
}

void btnfun_save_cb(void *arg, void *usr_data)
{
	save_flag = 1;
}

/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  system_clock_config();

  at32_board_init();
	
	crm_clocks_freq_get(&crm_clocks_freq_struct);
	led_gpio_init();
	led_tmr_init();
	ok_led_init();
	temperaure_sensor_init();
	
	eeprom_i2c_init();
	
	eeprom_read_int16(0x0, &LEDA_value);
	eeprom_read_int16(0x8, &LEDB_value);
	
	for(uint16_t i = 0; i <= LEDA_value; i++)
	{
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, i);
		delay_us(200);
	}
	for(uint16_t i = 0; i <= LEDB_value; i++)
	{
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, i);
		delay_us(200);
	}
	
	// create gpio button
	button_config_t gpio_btn_cfg = {
		.type = BUTTON_TYPE_GPIO,
		.long_press_time = 1300,
		.short_press_time = 180,
		.gpio_button_config = {
			.gpio_num = 0,
			.active_level = 0,
		},
	};
	button_handle_t gpio_btnAadd = iot_button_create(&gpio_btn_cfg);
	iot_button_register_cb(gpio_btnAadd, BUTTON_SINGLE_CLICK, btnAadd_single_click_cb, NULL);
	
	gpio_btn_cfg.gpio_button_config.gpio_num = 1;
	button_handle_t gpio_btnAremove = iot_button_create(&gpio_btn_cfg);
	iot_button_register_cb(gpio_btnAremove, BUTTON_SINGLE_CLICK, btnAremove_single_click_cb, NULL);

	gpio_btn_cfg.gpio_button_config.gpio_num = 2;
	button_handle_t gpio_btnfun = iot_button_create(&gpio_btn_cfg);
	iot_button_register_cb(gpio_btnfun, BUTTON_LONG_PRESS_START, btnfun_sleep_cb, NULL);
	iot_button_register_cb(gpio_btnfun, BUTTON_SINGLE_CLICK, btnfun_baoshan_cb, NULL);
	iot_button_register_cb(gpio_btnfun, BUTTON_DOUBLE_CLICK, btnfun_save_cb, NULL);
	
	gpio_btn_cfg.gpio_button_config.gpio_num = 3;
	button_handle_t gpio_btnBadd = iot_button_create(&gpio_btn_cfg);
	iot_button_register_cb(gpio_btnBadd, BUTTON_SINGLE_CLICK, btnBadd_single_click_cb, NULL);
	
	gpio_btn_cfg.gpio_button_config.gpio_num = 4;
	button_handle_t gpio_btnBremove = iot_button_create(&gpio_btn_cfg);
	iot_button_register_cb(gpio_btnBremove, BUTTON_SINGLE_CLICK, btnBremove_single_click_cb, NULL);
	
  while(1)
  {
		if(baoshan_flag){
			baoshan_i = !baoshan_i;
			if(baoshan_i){
				tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, 0);
				tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, 0);
			}else{
				tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, LEDA_value);
				tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, LEDB_value);
			}
			delay_ms(100);
		}
		if(save_flag)
		{
			save_flag = 0;
			eeprom_write_int16(0x0, LEDA_value);
			delay_ms(5);
			eeprom_write_int16(0x8, LEDB_value);
			for(uint8_t i = 0;i < 6;i++)
			{
				gpio_bits_write(GPIOA, GPIO_PINS_15, !gpio_output_data_bit_read(GPIOA, GPIO_PINS_15));
				delay_ms(200);
			}
		}
  }
}